/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.controller.user;

import akka.actor.ActorRef;
import com.cxhd.pacs.StaticData;
import com.cxhd.pacs.actor.ActorManager;
import com.cxhd.pacs.controller.service.AskService;
import com.cxhd.pacs.external.IntefaceOther;
import com.cxhd.pacs.model.Asklist;
import com.cxhd.pacs.model.FileDetail;
import com.cxhd.pacs.model.HostDetail;
import com.cxhd.pacs.model.HostInfo;
import com.cxhd.pacs.model.LisDetail;
import com.cxhd.pacs.tools.CodeUtil;
import com.cxhd.pacs.tools.DateUtil;
import com.cxhd.pacs.tools.FileUtil;
import com.cxhd.pacs.tools.ImageRender;
import com.cxhd.pacs.tools.PDFRender;
import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 *
 * @author 52461
 */
public class OtherCtrl extends Controller {

    HostInfo hi = new HostInfo();

    public void index() {

    }

    public void getResultItem() {
        Record re = Db.findFirst("select * from host_detail where pk =" + getPara("result_pk"));
        renderJson(re);
    }

    public void openResult() {
        setAttr("result_pk", getPara("result_pk"));
        render("/other/resultPage.jsp");
    }

    public void openMobileDetail() {
        HostInfo info = hi.getPatientInfo(getPara("host_id"));
        setAttr("patientName", info.get("name"));
        setAttr("item", JsonKit.toJson(info));

        setAttr("host_id", getPara("host_id"));
        render("/other/moblieDetail.jsp");
    }

    public void queryRpt() {
        //setAttr("result_pk", getPara("result_pk"));
        render("/other/queryRpt.jsp");
    }

    public void getImage() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new ImageRender(myFile, ""));

    }

    public void getPdf() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new PDFRender(myFile, ""));

    }

    public void getVideo() {
        String fileId = getPara("fileId");
        FileDetail mo = FileDetail.dao.getFile(fileId);
        File myFile = null;
        System.out.println(mo.getStr("type"));

        myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));

        System.out.println(myFile.getAbsoluteFile());
        render(new PDFRender(myFile, ""));

    }

    public void getLisResult() {

        renderJson(LisDetail.dao.getDetail(getPara("pk")));

    }
    HostDetail hd = new HostDetail();

    public void getResult() {
        if (getPara("fileId") != null && getPara("fileId").length() < 20 && getPara("type").equalsIgnoreCase("html")) {
            String fileId = getPara("fileId");
            FileDetail mo = FileDetail.dao.getFile(fileId);
            File myFile = null;
            System.out.println(mo.getStr("type"));

            myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));
            renderText(FileUtil.outWriter(myFile.getAbsolutePath()));
        } else {
            renderText(hd.getResult(getPara("pk")));
        }
    }

    public void getPdfToImage() {
        String fileId = getPara("fileId");
        FileDetail fd = new FileDetail().getFile(fileId);
        String path = fd.get("base_path") + "/" + fd.get("path") + "";
        System.out.println(path);
        File[] fl = new File(path).getParentFile().listFiles(new FileFilter() {

            public boolean accept(File f) {
                if (f.getName().endsWith(".jpg")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        List<String> listf = new ArrayList<String>();
        if (fl != null && fl.length > 0) {
            for (File f : fl) {
                listf.add(f.getAbsolutePath().replace("\\", "/"));
            }
        }
        renderJson(listf);
    }

    public void getPdfImage() {
        System.out.println(getPara("path"));
        render(new ImageRender(new File(getPara("path")), null));
    }

    public void openApply() {
        setAttr("askInfo", JsonKit.toJson(Db.findById("asklist", "pk", getPara("p"))));
        setAttr("contact_id", getPara("u"));
        render("/other/phoneViewDetail.jsp");
    }

    public void getDoctor() {
        renderJson(IntefaceOther.getDoctorByGroup(StaticData.DOCTOR_GROUP_NAME));
    }

    public void updateAskDetail() {
        // Asklist rd = new Asklist();

//        rd.findById(getPara("pk"))
//                .set("recvicer_id", getPara("recvicer_id"))
//                .set("recvicer_phone", getPara("recvicer_phone"))
//                .set("recvicer_name", getPara("recvicer_name"))
//                .set("appointment_time", getPara("appointment_time"))
//                .set("contact_id", getPara("contact_id"))
//                .update();
        String data = new AskService().updateAskRecvier(getPara("recvicer_id"), getPara("recvicer_phone"), getPara("recvicer_name"), getPara("appointment_time"), getPara("contact_id") + "", null, null, getPara("ask_code"));
        if (data != null && data.equalsIgnoreCase("true")) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("msgType", "sendDoctorAndsender");
            for (String key : getParaMap().keySet()) {
                map.put(key, getPara(key));
            }
            ActorManager.getIntance().getActor("sendSmsActor").tell(map, ActorRef.noSender());
        }
        renderText(data);
    }

    public void getBaseUrl() {
        Record re = Db.findFirst("select name,value from config where name = '" + getPara("uuid") + "'");
        if (re != null) {
            renderText(re.get("value") + "");
        } else {
            Record r = new Record();
            r.set("name", getPara("uuid"));
            r.set("value", "http://222.219.119.189:10218");
            renderText("http://222.219.119.189:10218");
        }

    }

    public void checkInfo() {

        Record re = Db.findFirst("select * from hostinfo where name='" + getPara("name") + "' and phone='" + getPara("phone") + "' ");

        if (re != null && re.get("phone") != null) {
            String code = CodeUtil.getSixNum();
            setSessionAttr("yzm", code);
            setSessionAttr("info", re);
            Map<String, String> map = new HashMap<String, String>();
            map.put("msgType", "queryRpt");
            map.put("to", re.get("phone") + "");
            map.put("yzm", code);
            System.out.println(map);
            ActorManager.getIntance().getActor("sendSmsActor").tell(map, ActorRef.noSender());
            renderJson(re);
        } else {
            renderText("false");
        }

    }

    public void getMobileDetail() {
        renderJson(Db.find("select * from host_detail where host_id = '" + getPara("host_id") + "' order by host_id desc "));
    }

    public void getMobileDetailFilter() {
        renderJson(Db.find("select * from host_detail where host_id = '" + getPara("host_id") + "' and event_name='" + getPara("event_name") + "' order by event_time desc "));
    }

    public void checkyzm() {
        String yzm = (String) getSessionAttr("yzm");
        Record info = (Record) getSessionAttr("info");
        System.out.println(yzm);
        System.out.println(info);
        if (getPara("yzm") != null && getPara("yzm").equalsIgnoreCase(yzm)) {
            renderJson(info);
        } else {
            renderText("flase");
        }
    }

    public void getPatientList() {
        String sql = "select  host_id,name,sex,age,birth,phone,hospital_code,info_id ";
        String serarchContent = getPara("content", null);

        String search1 = "";

        System.out.println(search1);
        // String param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code " + search1.replaceFirst("and", "") + " group by a.host_id  order by a.host_id desc";
        String param = "from hostinfo   ";

// String param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code " + search1.replace("and", "") + " group by b.host_id  order by b.host_id desc";
        if (serarchContent != null) {

            param = "from  hostinfo where host_id = '" + serarchContent + "' or name = '" + serarchContent + "' or info_id = '" + serarchContent + "' ";
            //param = "from  host_detail a left join hostinfo b on a.host_id=b.host_id left join hospital c on a.hospital_code = c.hospital_code where (a.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or a.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by a.host_id  order by a.host_id desc";

            //param = "from  hostinfo b left join hospital c on b.hospital_code = c.hospital_code where (b.host_id like '%" + serarchContent + "%' or  b.name like '%" + serarchContent + "%'  or b.phone like '%" + serarchContent + "%'  or b.card_no like '%" + serarchContent + "%' or b.pym like '%" + serarchContent + "%' or b.info_id like '%" + serarchContent + "%') " + search1.replace("where", "") + " group by b.host_id order by b.host_id desc";
        }
        System.out.println(sql + param);
        Page<Record> userPage = Db.paginate(getParaToInt("currentPage", 1), getParaToInt("pageSize", 10), sql, param);

        renderJson(userPage);
    }

    public void getPatientDetail() {

        renderJson(Db.find("select * from host_detail where host_id='" + getPara("host_id") + "'"));
    }

    public void detailResult() {
        if (getPara("fileId") != null && getPara("fileId").length() < 20 && getPara("type").equalsIgnoreCase("html")) {
            String fileId = getPara("fileId");
            FileDetail mo = FileDetail.dao.getFile(fileId);
            File myFile = null;
            System.out.println(mo.getStr("type"));

            myFile = new File(mo.getStr("base_path") + "/" + mo.getStr("path"));
            renderText(FileUtil.outWriter(myFile.getAbsolutePath()));
        } else {
            renderText(hd.getResult(getPara("pk")));
        }
    }

    public void makeDataForDepartment() {

        List<String> list = new ArrayList<String>();
        //:内科、外科、儿科、妇科、眼科、耳鼻喉科、口腔科、皮肤科、中医科
        list.add("内科");
        list.add("外科");
        list.add("儿科");
        list.add("妇科");
        list.add("耳鼻喉科");
        list.add("中医科");

        List<Record> list_info = Db.find("select * from hostinfo ");

        for (Record ind : list_info) {

            Record de = new Record();
            de.set("host_id", ind.get("host_id"));
            de.set("info_id", ind.get("info_id"));
            de.set("createtime", DateUtil.getDateEN());
            de.set("department", list.get(new Random().nextInt(list.size())));
            Db.save("host_in_hospital_detail", de);

        }
        
        renderText("true");

    }
    public void getDepartment(){
     
        renderJson(Db.find("select department from host_in_hospital_detail group by department"));
    }
    
    public void getPatientForDepartment(){
        renderJson(Db.find("select b.*,a.department from host_in_hospital_detail a left join hostinfo b using(host_id)  where a.department='"+getPara("department")+"' limit 100"));
    }
    

}
